<template>
  <div class="com-container">
    <div class="input-box">
      <el-row>
        <el-col :span="4">
          <div class="icon-left">
            <el-button type="primary" icon="el-icon-arrow-left" size="mini" @click="leftClick"></el-button>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="year-input">
            <el-input v-model="startYear" placeholder="开始年份"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="year-input">
            <el-input v-model="endYear" placeholder="结束年份"></el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="icon-right">
            <el-button type="primary" icon="el-icon-arrow-right" size="mini" @click="rightClick"></el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import indexdata from '@/api/data/indexdata'

export default {
  name: 'TimeBar',
  props: ['timecity'],
  watch: {
    timecity: {
      async handler (newVal) {
        this.startYear = newVal.startYear + '年'
        this.endYear = newVal.endYear + '年'
      },
      deep: true
    }
  },
  data () {
    return {
      startYear: '',
      endYear: '',
      limitYear: {}
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    async init () {
      const res = await indexdata.getMinMaxYear()
      this.startYear = res.data.minMaxYear.minYear + '年'
      this.endYear = res.data.minMaxYear.maxYear + '年'
      // 存储最大最小年份
      this.limitYear = res.data.minMaxYear
    },
    leftClick () {
      let year =parseInt(this.startYear.substring(0, this.startYear.length - 1))
      if(year > this.limitYear.minYear){
        year = year - 1
        this.startYear = year + '年'
      }
    },
    rightClick () {
      let year = parseInt(this.endYear.substring(0, this.endYear.length - 1))
      if(year < this.limitYear.maxYear){
        year = year + 1
        this.endYear = year + '年'
      }
    }
  }
}
</script>

<style scoped>

.input-box {
  text-align: center;
  background-color: #161522;
  border-radius: 20px;
  position: absolute;
  width: 20%;
  margin-left: 10px;
}

.icon-left {
  position: relative;
  margin-left: 5px;
}

.icon-right {
  position: relative;
  margin-left: 5px;
}

.year-input {
  position: relative;
  margin-left: 5px;
}

.el-button {
  line-height: 2;
}

.el-button--primary {
  background-color: #409F02;
  border-color: #409F02;
}
</style>
